Does anyone see any issues with the code or with this approach.
#include "KMotionDef.h"
#include "PC-DSP.h"
#define TMP 10
#include "KflopToKMotionCNCFunctions.c"
double DROx, DROy, DROz, DROa, DROb, DROc;
void SetSoftLim(void);
void home(void);
void estop(void);
void SetZ(void);
void RST_ENC(void);
void Disable(void);
void Enable(void);
int enable=0;
int SL=-1;
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define XDrive 152
#define YDrive 153
#define ZDrive 154
#define Lube 155
#define spindle 0
#define spindle_CLK 144
#define spindle_COCLK 146
#define ESTOP_BIT 139
#define ZLIM_BIT 139
#define XLIM_BIT 138
#define YLIM_BIT 137
#define X 0
#define Y 1
#define Z 2
#define MaxY 130000
#define MinY -1000
#define MaxZ 0
#define MinZ -80000
#define MaxX 196000
#define MinX -1000
void main(void){
Disable();
printf("Thread 2 init\n");
while(1){
estop();
SetSoftLim();
home();
RST_ENC();
WaitNextTimeSlice();
}
}
void estop(void){
if(ch0->Enable || ch1->Enable || ch2->Enable){
if (!ReadBit(ESTOP_BIT) || !ReadBit(ZLIM_BIT) || !ReadBit(YLIM_BIT) || !ReadBit(XLIM_BIT) )
{
Disable();
printf("Spindle off drives disabled E Stop\n");
}
else if(ReadBit(55)) {
Disable();
printf("Spindle off drives disabled you pushed axis disable button\n");
}
}
else if(ReadBit(55) && ReadBit(ESTOP_BIT) && ReadBit(ZLIM_BIT) && ReadBit(YLIM_BIT) && ReadBit(XLIM_BIT) ){
Enable();
printf("Spindle and drives enabled you pushed axis enable button\n");
}
}
void SetSoftLim(void){
if(ReadBit(48) && SL) {
ClearStopImmediately();
//disable soft limits
ch0->SoftLimitPos= 800000;
ch0->SoftLimitNeg=-800000;
ch1->SoftLimitPos=800000;
ch1->SoftLimitNeg=-800000;
ch2->SoftLimitPos=800000;
ch2->SoftLimitNeg=-800000;
ClearBit(48);
SL=0;
printf("soft limits disabled \n");
}
else if (ReadBit(48) && !SL || SL==-1)
{
//enable soft limits
ch0->SoftLimitPos=MaxX;
ch0->SoftLimitNeg=MinX;
ch1->SoftLimitPos=MaxY;
ch1->SoftLimitNeg=MinY;
ch2->SoftLimitPos=MaxZ;
ch2->SoftLimitNeg=MinZ;
ClearBit(48);
SL=1;
printf("soft limits enabled \n");
}
return;
}
void SetZ(void){
if(ReadBit(53)) {
ch2->SoftLimitNeg=ch2->Dest;
ClearBit(53);
}
if(ReadBit(54)) {
ch2->SoftLimitNeg=MinZ;
ClearBit(54);
}
return;
}
void home (void){
if(ReadBit(49)){ //home sequence X,Y
ClearStopImmediately();
Zero(X);
Zero(Y);
ClearBit(49);
}
if(ReadBit(52)){ //home sequence Z
ClearStopImmediately();
Zero(Z);
ClearBit(52);
}
return;
}
}
void RST_ENC(void){
if(ReadBit(56)){
EnableAxisDest(X,ch0->Position);
EnableAxisDest(Y,ch1->Position);
EnableAxisDest(Z,ch2->Position);
ClearBit(56);
}
return;
}
void Enable(void){
enable=1;
EnableAxisDest(Z,ch2->Dest); //Z
EnableAxisDest(Y,ch1->Dest); //Y
EnableAxisDest(X,ch0->Dest); //X
SetBit(XDrive);
SetBit(154);
SetBit(153);
SetBit(155);
ClearBit(55);
ClearBit(145);
ClearBit(144);
ClearBit(146);
printf("Spindle and drives Enabled\n");
DAC(0,0);
ch0->SoftLimitPos=MaxX;
ch0->SoftLimitNeg=MinX;
ch1->SoftLimitPos=MaxY;
ch1->SoftLimitNeg=MinY;
ch2->SoftLimitPos=MaxZ;
ch2->SoftLimitNeg=MinZ;
SL=1;
printf("soft limits enabled \n");
ClearBit(48);
}
void Disable(void){
DisableAxis(Z); // axis still enabled? - Disable it
DisableAxis(Y); // axis still enabled? - Disable it
DisableAxis(X); // axis still enabled? - Disable it
ClearBit(55);
ClearBit(XDrive);
ClearBit(154);
ClearBit(153);
ClearBit(155);
SetBit(145);
ClearBit(146);
ClearBit(144);
DAC(0,0);
enable=0;
}